package com.atguigu.gmall.common.config;

import com.atguigu.gmall.common.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

//注意:一定要保证启动类 扫描到该配置类
@Slf4j
@ResponseBody
@ControllerAdvice
public class GlobalExceptionHandler {

    //可以有自定义异常
    //处理运行时异常
    @ExceptionHandler(RuntimeException.class)
    public Result exceptionError(RuntimeException e){
        log.error("全局异常RuntimeException处理:{}",e);
        return Result.ok().fail().message(e.getMessage());
    }

    //处理各种类型异常
    @ExceptionHandler(Exception.class)
    public Result exception(Exception e){
        log.error("全局异常处理：{}",e);
        return Result.ok().fail().message(e.getMessage());
    }
}
